<?php
include "connectDB.php";
$fun = $_POST["function"];
if($fun == 'item_add'){item_add();}
else if($fun == 'item_show_edit'){item_show_edit();}
else if($fun == 'item_del' ){item_del();}
else if($fun == 'edit_item_save'){edit_item_save();}
else if($fun == 'show_table_1'){show_table_1();}
function item_add(){
    $merchant=$_POST["merchant"];
    $strSQL = "INSERT INTO tbl_item (item_name,price,mer_id)";
    $strSQL .= " VALUES (";
    $strSQL .= "'".$_POST["txtItemName"]."',";
    $strSQL .= "'".$_POST["txtPrice"]."',";
    $strSQL .= "'".$_POST["merchant"]."'";
    $strSQL .= ")";
    $objQuery = mysql_query($strSQL);
    //echo 'Save';
    show_table_1();
    //show_table();
}
function item_del(){
    $strSQL = 'DELETE FROM tbl_item WHERE item_id = ';
    $strSQL .= $_POST["item_id"];
    $objQuery = mysql_query($strSQL);
    //echo $strSQL;
    show_table_1();
    //echo 'Del Complete';
}
function edit_item_save(){
    $item_id=$_POST["item_id"];
    $item_name=$_POST["item_name"];
    $price=$_POST["price"];
    $mer_id=$_POST["mer_id"];

    $strSQL = "UPDATE tbl_item SET ";
    $strSQL .= "item_name = '".$item_name."',";
    $strSQL .= "price = ".$price.",";
    $strSQL .= "mer_id = ".$mer_id;
    $strSQL .= " WHERE item_id = ".$item_id;
    $objQuery = mysql_query($strSQL);
    show_table_1();
}
function item_show_edit(){
    $item_id=$_POST["item_id"];
    $strSQL = "SELECT * FROM 'tbl_item' ";
    $strSQL .= " WHERE item_id =";
    $strSQL .= $item_id;
    $objQuery = mysql_query($strSQL);
    echo $objQuery;

    $sql ="SELECT * FROM tbl_item WHERE item_id =$id";
    $result = mysql_db_query('machine',$strSQL);
    while($rs=mysql_fetch_array($result)) {
      $item_name = $rs["item_name"];
      $price = $rs["price"];
      $item_id = $rs["item_id"];
      echo $item_id;
    }
}
/*
function show_table(){


    $strSQL = "SELECT * FROM tbl_item INNER JOIN tbl_merchant on tbl_merchant.mer_id=tbl_item.mer_id ORDER BY tbl_item.date DESC";
    $objQuery = mysql_query($strSQL);

   while ($objResult = mysql_fetch_array($objQuery))
   {



          echo "<tr>";
          echo "<td>".$objResult['item_id']."</td>";
          echo "<td>".$objResult['item_name']."</td>";
          echo "<td>".$objResult['price']."</td>";
          echo "<td>".$objResult['date']."</td>";
          echo "<td>".$objResult['mer_name']."</td>";

          //echo "<td><a href="JavaScript:if(confirm('Comfirm Edit Data?')==true){edit_item(".$objResult["item_id"].",'".$objResult["item_name"]"',".$objResult["price"].",".$objResult["mer_id"].");}>Edit</a></td>";
          //echo '<td><a href="JavaScript:if(confirm('Comfirm Delete?')==true){del_item(<?=$objResult["item_id"];?>);}">Del</a></td>';
     </tr>

   }

}

function show_table_1(){
?>
        <table border="0" cellpadding="10" cellspacing="5">
                <tr>
                        <td>Merchant ID</td>
                        <td>Merchant Name</td>
                        <td>Description</td>
                        <td>Longtitude</td>
                        <td>Latitude</td>
                        <td>Date</td>
                        <td>E</td>
                        <td>D</td>
                </tr>
     <?php
         $strSQL = "SELECT * FROM tbl_item INNER JOIN tbl_merchant on tbl_merchant.mer_id=tbl_item.mer_id ORDER BY tbl_item.date DESC";
         $objQuery = mysql_query($strSQL);

                while ($objResult = mysql_fetch_array($objQuery))
                {
         ?>

                <tr>
                        <td><?php echo $objResult['mer_id'];?></td>
                        <td><?php echo $objResult['mer_name'];?></td>
                        <td><?php echo $objResult['description'];?></td>
                        <td><?php echo $objResult['longtitude'];?></td>
                        <td><?php echo $objResult['latitude'];?></td>
                        <td><?php echo $objResult['date'];?></td>
                <!--<td><a href="JavaScript:if(confirm('Comfirm Edit Data?')==true){window.location='EditMerchant.php?MerID=<?=$objResult["mer_id"];?>';}">Edit</a></td> -->
                        <td><input type="button" name="edit-btn" id="edit-btn"  onclick="editMer()" value="edit" /></td>
                <!--<td><a href="JavaScript:if(confirm('Confirm Delete?')==true){window.location='DeleteMerchant.php?MerID=<?=$objResult["mer_id"];?>';}">Delete</a></td> -->
                    <td><input type="button" name="del-btn" id="del-btn"  onclick="delMer(<?php echo $tid ;     ?>)" value="del" /></td>

                </tr>
        <?php
                }
        ?>
    </table>
        <?php
}
*/
function show_table_1(){
?>
        <table id="example" width="100%" border="0" cellpadding="10" cellspacing="5">
                <thead >
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5">ItemName</td>
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5">Price</td>
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5">Date</td>
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5">MerName</td>
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5"><center>Edit</center></td>
                  <td style="background-color: #e7e1e1;font-weight:bold;border:solid 2px; border-color:#ddd5d5"><center>Delete</center></td>
                </thead>
                <tbody>
     <?php
         $strSQL = "SELECT * FROM tbl_item INNER JOIN tbl_merchant on tbl_merchant.mer_id=tbl_item.mer_id ORDER BY tbl_item.date DESC";
         $objQuery = mysql_query($strSQL);

                while ($objResult = mysql_fetch_array($objQuery))
                {
         ?>

                <tr>
                  <td><?php echo $objResult['item_name'];?></td>
                  <td><?php echo $objResult['price'];?></td>
                  <td><?php echo $objResult['date'];?></td>
                  <td><?php echo $objResult['mer_name'];?></td>
                  <td><center><a style="color:	#ffb6ad;font-weight:bold;" href="JavaScript:if(confirm('Comfirm Edit Data?')==true){edit_item(<?=$objResult["item_id"];?>,'<?=$objResult["item_name"];?>',<?=$objResult["price"];?>,<?=$objResult["mer_id"];?>);}">Edit</a></center></td>
                  <td><center><a style="color:#ff6569;font-weight:bold;" href="JavaScript:if(confirm('Comfirm Delete?')==true){del_item(<?=$objResult["item_id"];?>);}">Del</a></center></td>
                </tr>
        <?php
                }
        ?>
      </tbody>
    </table>
        <?php
}
        ?>
